#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdbool.h>
#include<stdlib.h>
typedef int type;
 
typedef struct QueueNode
{
	type data;
	struct QueueNode* next;
}QueueNode,*pQueueNode;

//typedef struct Queue
//{
//	pQueueNode front;
//	pQueueNode tail;
//	int size;
//}Queue;


typedef struct {
	pQueueNode front;
	pQueueNode tail;
	int size;
	int capatity;
} MyCircularQueue;


MyCircularQueue* myCircularQueueCreate(int k) {
	MyCircularQueue* obj = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
	obj->capatity = k;
	obj->front = obj->tail = NULL;
	obj->size = 0;
	return obj;
}

bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
	if (obj->size == obj->capatity)
	{
		return false;
	}

	pQueueNode newnode = (pQueueNode)malloc(sizeof(QueueNode));
	newnode->data = value;
	newnode->next = NULL;
	if (obj->front = NULL)
	{
		obj->front = obj->tail = newnode;
	}
	else
	{
		obj->tail->next = newnode;
		obj->tail = newnode;
	}
	obj->size++;
	return true;
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
	if (obj->size == 0)
	{
		return false;
	}
	pQueueNode cur = obj->front->next;
	free(obj->front);
	obj->front = NULL;
	obj->front = cur;
	obj->size--;
	return true;
}

int myCircularQueueFront(MyCircularQueue* obj) {
	if (obj->size == 0)
	{
		return -1;
	}
	return obj->front->data;
}

int myCircularQueueRear(MyCircularQueue* obj) {
	if(obj->size == 0)
	{
		return -1;
	}
	return obj->tail->data;
}

bool myCircularQueueIsEmpty(MyCircularQueue* obj) {

	return obj->size == 0;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
	return obj->size == obj->capatity;
}

void myCircularQueueFree(MyCircularQueue* obj) {
	while (obj->front)
	{
		pQueueNode cur = obj->front->next;
		free(obj->front);
		obj->front = NULL;
		obj->front = cur;
	}
	free(obj);
}
